<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class City extends CI_Model
{
	protected $table = 'city';
	protected $reltable = 'incity';
	protected $cittable = 'citizen';
	
	public function loadCity( $id )
	{	
		return $this->db->select('*')
						->from( $this->table )
						->where( 'id', $id )
						->get()
						->result();
	}
	
	public function addCity( $city )
	{
		$res = $this->db->select( 'id' )
						->from( $this->table )
						->where( 'id', $city['id'] )
						->get()
						->result();
						
		
		
			
		if( empty( $res ) )
		{ // add to db
			$this->db->set( 'id', $city['id'] );
			$this->db->set( 'name', $city['name'] );			
			$this->db->insert( $this->table );
		}
		return $city;
	}
	
	public function loadCitizens( $id )
	{
		// TODO : join
	
		$res = $this->db->select( )
						->from( $this->reltable )
						->where( 'id_city', $id )
						->get()
						->result();
		
		$i = 0;
		$citizens = array();
		foreach( $res as $cit )
		{
			$citizen = $this->db->select( )
								->from( $this->cittable )
								->where( 'id', $cit->id_citizen )
								->get()
								->result();
			$citizens[$i]['id'] = $citizen[0]->id;
			$citizens[$i]['nickname'] = $citizen[0]->nickname;
			$citizens[$i]['avatar'] = $citizen[0]->avatar;
			$citizens[$i]['vote_pos'] = $citizen[0]->vote_pos;
			$citizens[$i]['vote_neg'] = $citizen[0]->vote_neg;
			$citizens[$i]['score'] = $citizen[0]->score;
			$i++;
		}
		return $citizens;
	}
}


/* End of file city.php */
/* Location: ./application/models/city.php */